<template>
  <div>
    <el-button icon="el-icon-mobile" type="primary" plain size="medium" @click="onSave()" :disabled="!zIsOpenData">
      保存接口权限</el-button>
    <el-button icon="el-icon-refresh" type="primary" plain size="medium" @click="openData(true)">刷新接口权限</el-button>
    <el-button icon="el-icon-setting" type="primary" plain size="medium" @click="initControllerToDB()">初始接口权限
    </el-button>
    <div style="height: 15px"></div>
    <el-tree :data="FRoleRightList" show-checkbox node-key="FRightID" ref="refRightTree" highlight-current
      :props="FTreeProps" accordion>
      <template #default="{ node, data }">
        <el-row :gutter="20" style="width: 100%;">
          <el-col :span="15">
            <span style="width: 250px">{{ node.data.FRightName }}</span>
          </el-col>
          <el-col :span="5">
            <div style="float:right;">
              <el-button plain type="primary" size="mini" @click="onNodeClick(node, data)">信息编辑</el-button>
            </div>
          </el-col>
        </el-row>
      </template>
    </el-tree>
    <el-dialog v-model="FIsShowRightEidt" width="400PX" center destroy-on-close>
      <TFormRightEdit :zContextParams="contextParams" :zContextOnCall="onCallBack"></TFormRightEdit>
    </el-dialog>
  </div>
</template>
<script lang="ts">
  import { Options } from "vue-class-component";
  import TFormBase from "@/views/BaseForm";
  import {
    TBaseRight,
    TBaseRole,
    TRoleRightItem,
  } from "@/baselib/right/BaseRightClass";
  import elmHelper from "@/views/model/elmHelper";
  import TFormRightEdit from "./RightEdit.vue";

  //配合着用
  @Options({
    name: "TFormRoleRightApi",
    components: { TFormRightEdit },
    computed: {},
    data() { },
  })
  export default class TFormRoleRightApi extends TFormBase {
    private get contextParams() {
      return { rightID: this.FRightID };
    }
    declare $refs: {
      refRightTree: any;
    };
    private FIsShowRightEidt: boolean = false;
    private FRightID: string = "";
    private FIndeRole: TBaseRole = new TBaseRole();
    private FTreeProps = { label: "FRightName", children: "rightChildren" };
    private FRoleRightList: TRoleRightItem[] = [];
    public created() {
      this.FIndeRole = this.zContextParams;
      if (!this.zIsOpenData) {
        this.openData();
      }
    }
    private async openData(qHint: boolean = false) {
      this.zIsOpenData = false;
      var lResult = await this.$myBaseRightAPI.getRoleRightList(
        this.FIndeRole.FRoleID
      );
      if (!lResult.resultSuccess) {
        this.$myMsgHelp.msgDialog(lResult.resultMsg);
        return;
      }
      //
      this.FRoleRightList = lResult.resultData;
      //启用,禁用
      let lChecks = elmHelper.rightListGetCheckKeys(this.FRoleRightList);
      this.$refs.refRightTree.setCheckedKeys(lChecks);
      this.zIsOpenData = true;
      if (qHint) {
        this.$myMsgHelp.msgHintAutoClose(
          "刷新菜单成功",
          this.$myMsgHelp.msgSuccess
        );
      }
    }
    private async initControllerToDB() {
      var lResult = await this.$myBaseRightAPI.getControllerToDB();
      if (!lResult.resultSuccess) {
        this.$myMsgHelp.msgDialog(lResult.resultMsg);
        return;
      } else {
        this.$myMsgHelp.msgHintAutoClose(
          "初始化控制器权限成功",
          this.$myMsgHelp.msgSuccess
        );
        this.openData();
      }
    }
    private async onSave() {
      let lChecks = this.$refs.refRightTree.getCheckedKeys();
      if (lChecks == null || lChecks.length == 0) {
        let lConfirm = await this.$myMsgHelp.msgDialogConfirm(
          "确定要清除此角色所有菜单"
        );
        if (!lConfirm) {
          return;
        }
      }
      let lResult = await this.$myBaseRightAPI.saveRoleRigt(
        this.FIndeRole.FRoleID,
        lChecks
      );
      if (!lResult.resultSuccess) {
        this.$myMsgHelp.msgDialog(lResult.resultMsg);
        return;
      } else {
        this.$myMsgHelp.msgHintAutoClose(
          "保存角色菜单权限成功",
          this.$myMsgHelp.msgSuccess
        );
      }
    }
    private onNodeClick(node: any, data: any) {
      this.FRightID = node.key;
      this.FIsShowRightEidt = true;
    }
    //编辑界面回调列表事件
    private onCallBack(qAction: string, qCompany: TBaseRight) {
      if (qAction == "close") {
        this.FIsShowRightEidt = false;
      }
      if (qAction == "save") {
        this.FIsShowRightEidt = false;
      }
    }
  }
</script>